package com.coin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.Config;
import com.coin.model.R;
import com.coin.service.ConfigService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Auther: 李 力
 * @Date: 2025/6/6
 * @Description: com.coin.controller
 * @version: 1.0
 */
@Api(tags = "config的控制器")
@RestController
@RequestMapping("/configs")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    /**
     * @Description: 参数配置查询
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param:
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "条件分页查询后台参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "后台参数类型"),
            @ApiImplicitParam(name = "code", value = "后台参数code"),
            @ApiImplicitParam(name = "name", value = "后台参数名称"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),

    })
    @PreAuthorize("hasAuthority('config_query')")
    public R<Page<Config>> findByPage(@ApiIgnore Page<Config> page, String type, String code, String name) {
        Page<Config> configPage = configService.findByPage(page, type, name, code);
        return R.ok(configPage);
    }

    /**
     * @Description: 新增一个参数
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: config
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增一个参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "config", value = "config 的json数据")
    })
    @PreAuthorize("hasAuthority('config_create')")
    public R add(@RequestBody @Validated Config config) {
        boolean result = configService.saveConfig(config);
        return result ? R.ok() : R.fail("新增失败");
    }

    /**
     * @Description: 修改一个参数
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: config
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "修改一个参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "config", value = "config 的json数据")
    })
    @PreAuthorize("hasAuthority('config_update')")
    public R update(@RequestBody @Validated Config config) {
        boolean result = configService.updateConfig(config);
        return result ? R.ok() : R.fail("修改失败");
    }


}
